package entity;

import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import db.DBDataSource;
import db.Factor;

public class Youxiji {
	// 属性
	private PlayerEntity[] pl;

	public void doT() {
		int v = chushihua();
		print1();
		if (v == 1) {
			panding1();
		} else {
			panding2();
		}
	}

	/**
	 * 初始化玩家信息
	 */
	public int chushihua() {
		Set s = new HashSet();
		System.out.println("押1000以下是平民，1000以上是贵宾！贵宾可以在筛子点数上加1，但是最高不能超过18！");
		Scanner input = new Scanner(System.in);
		System.out.println("怎么玩？-------1.比谁大！2比谁小！");
		int v = input.nextInt();
		System.out.println("几个人玩这局游戏:");
		int i = input.nextInt();
		pl = new PlayerEntity[i];
		for (int a = 0; a < i; a++) {
			System.out.println("第" + a + "位玩家，你押多少？");
			int j = input.nextInt();
			if (j >= 1000) {
				PlayerEntity p = new Guibin();
				System.out.println("请输入姓名：");
				p.setPlName(input.next());
				System.out.println("请输入性别：");
				p.setPlsex(input.next());
				boolean isF = true;
				while (isF) {
					System.out.println("请输入游戏ID：");
					System.out.println("注意：游戏ID不能与别的玩家相同！");
					int t = input.nextInt();
					p.setPlyouxi_id(t);
					s.add(t);
					if (s.size() == a + 1) {
						isF = false;
					} else {
						System.out.println("你输入的游戏ID已经被人占用了！");
					}
				}
				p.setScore(p.doStart());
				pl[a] = p;
				String strSQL = "insert into player (PlName,Plsex,Plyouxi_id,Score,Pltime) values (?,?,?,?,sysdate) ";
				int l = Factor.getDBDriverInstance().update(strSQL,
						new Object[] { p.getPlName(), p.getPlsex(), p.getPlyouxi_id(), p.getScore() });
				System.out.println(l > 0 ? "数据增加成功" : "数据增加失败");
			} else {
				PlayerEntity p = new Pingmin();
				System.out.println("请输入姓名：");
				p.setPlName(input.next());
				System.out.println("请输入性别：");
				p.setPlsex(input.next());
				boolean isF = true;
				while (isF) {
					System.out.println("请输入游戏ID：");
					System.out.println("注意：游戏ID不能与别的玩家相同！");
					int t = input.nextInt();
					p.setPlyouxi_id(t);
					s.add(t);
					if (s.size() == a + 1) {
						isF = false;
					} else {
						System.out.println("你输入的游戏ID已经被人占用了！");
					}
				}
				p.setScore(p.doStart());
				pl[a] = p;
				String strSQL = "insert into player (PlName,Plsex,Plyouxi_id,Score,Pltime) values (?,?,?,?,sysdate) ";
				int l = Factor.getDBDriverInstance().update(strSQL,
						new Object[] { p.getPlName(), p.getPlsex(), p.getPlyouxi_id(), p.getScore() });
				System.out.println(l > 0 ? "数据增加成功" : "数据增加失败");
			}
		}
		String strSQL = "insert into player (PlName,Plsex,Plyouxi_id,Score,Pltime) values (?,?,?,?,?) ";
		int l = Factor.getDBDriverInstance().update(strSQL, new Object[] { null, null, null, null, null });
		System.out.println(l > 0 ? "数据增加成功" : "数据增加失败");
		return v;
	}

	/**
	 * 打印玩家信息
	 */
	public void print1() {
		for (int i = 0; i < pl.length; i++) {
			System.out.println("姓名为：" + pl[i].getPlName() + "  性别为：" + pl[i].getPlsex() + "  游戏ID为："
					+ pl[i].getPlyouxi_id() + "  点数为：" + pl[i].getScore());
		}
	}

	public void panding1() {
		int temp = 0;
		for (int i = 0; i < pl.length; i++) {
			if (temp < pl[i].getScore()) {
				temp = pl[i].getScore();
			}
		}
		for (int i = 0; i < pl.length; i++) {
			if (temp == pl[i].getScore()) {
				System.out.println(pl[i].getPlName() + "赢了");
			}
		}
	}

	public void panding2() {
		int temp = 19;
		for (int i = 0; i < pl.length; i++) {
			if (temp > pl[i].getScore()) {
				temp = pl[i].getScore();
			}
		}
		for (int i = 0; i < pl.length; i++) {
			if (temp == pl[i].getScore()) {
				System.out.println(pl[i].getPlName() + "赢了");
			}
		}
	}
}
